<?php
include('../../include/functions.inc.php');
$list = $db->query_select("cllg_career", "FK_ENTITY=" . $user->entity);
$listCycles = $db->query_select("cllg_college_cycle", "FK_ENTITY=" . $user->entity);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Asignacion de Estudiantes</title>
        <link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="../../css/ContentStyle.css" rel="stylesheet" type="text/css" />
        <link href="../../js/protocalendar/datepicker.css" rel="stylesheet" type="text/css" />
        <link href="../../css/tableorderer.css" rel="stylesheet" type="text/css" />
        <link href="../../js/dialog/dialog.2.0.css" rel="stylesheet" type="text/css" />
        <script src="../../js/prototype.js" type="text/javascript"></script>
        <script src="../../js/jquery.js" type="text/javascript"></script>
        <script>jQuery.noConflict();</script>
        <script src="../../bootstrap/js/bootstrap.js"></script>
        <script src="../../js/jsvalidate.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/prototype-date-extensions.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/datepicker.js"  type="text/javascript"></script>
        <script src="../../js/TableOrderer.js" type="text/javascript"></script>
        <script src="../../js/dialog/dialog.2.0.js" type="text/javascript"></script>
        <script src="../../js/include.js" type="text/javascript"></script>
        <style type="text/css">
            input,
            textarea{
                width: 300px;
            }
            select{
                width: 315px;
            }
        </style>
        <script type="text/javascript">
            var cargos=null;
            var cursos=new Hash();
            
            function checkPadre( intCiclo ) {
                if( jQuery("#parent"+intCiclo).is(':checked') ) {
                    jQuery("#SECTION_ciclo_"+intCiclo).attr('disabled',false);    
                }
                else {
                    jQuery("#SECTION_ciclo_"+intCiclo).attr('disabled',true);
                }
            }
            
            function cambiarSeccionAll() {
                jQuery("select[name*='SECTION_ciclo_']").val(jQuery("#SECTION_ALL").val());
                jQuery("select[name*='SECTION_course_']").val(jQuery("#SECTION_ALL").val());
            }
            
            function cambiarSeccion( intCiclo ) {
                jQuery("select[name*='SECTION_course_"+ intCiclo + "']").val(jQuery("#SECTION_ciclo_"+intCiclo).val());
            }

            datosCarrera=function(control){
                if(control.selectedIndex>0){
                    $('operation').value='refresh';
                    $(document.ASIGNACION).request({onComplete:function(r){
                            cargos=r.responseJSON;
                            perform();
                            showCargos();
                            showPensums();
                        }});
                }
            }

            showPensums=function(carrera){
                new Ajax.Updater("pensums","pensums.php",{parameters:"CARRERA="+$F("CARRERA")});
            }
            showCursos=function(pensum){
                new Ajax.Updater("cursosList","cursos.php",{parameters:"PENSUM="+$F("PENSUM")});
            }

            checkAll=function(chk){
                if(chk.checked){
                    Dialogs.confirm("Esta seguro de asignar todos los cursos?",
                    function(){
                        doAll(chk);
                        jQuery("select[id*='SECTION_ciclo_']").attr('disabled',false);
                    }, //yes
                    function(){  //no
                        chk.checked=false;
                    });
                }
                else {
                    doAll(chk);
                    jQuery("select[id*='SECTION_ciclo_']").attr('disabled',true);
                }
            }

            doAll=function(chk){
                $(chk.form).getInputs("checkbox").each(function(c){
                    c.checked=chk.checked;
                    viewChk(c);
                });
            }

            checkChilds=function(chk){
                var className=chk.id;
                $$("."+className).each(function(c){
                    c.checked=chk.checked;
                    viewChk(c);
                });
            }

            viewChk=function(c){
                var id="SECTION_"+c.id;
                if(c.checked && !c.id.startsWith("all") && !c.id.startsWith("parent")){
                    var curso={id:c.alt,section:$F(id)};
                    cursos.set(c.alt,curso);
                    $(id).disabled=false;
                }else{
                    cursos.unset(c.alt);
                    if($(id)!=null)
                        $(id).disabled=true;
                }
            }

            updateCurso=function(c){
                cursos.set(c.id,c);
            }

            perform=function(){
                cargos.each(function(r){
                    r.FRECUENCIA=getFrecuencia(r.FRECUENCIA);
                });
            }

            showCargos=function(){
                new TableOrderer('cargos',{data:cargos});
            }

            datosEstud=function(e){
                $('operation').value=(e.id==2)?'aiu':'id';
                $(document.ASIGNACION).request({onComplete:function(r){
                        res=r.responseJSON;
                        if(res.ERROR){
                            alert(res.ERROR);
                        }else{
                            $('NAME').value=res.NAME;
                            $('ID').value=res.ID;
                            $('AIU_CODE').value=res.AIU;
                        }
                    }});
            }

            var rsv;
            config=function(){
                rsv=new RSV({
                    formID: $(document.ASIGNACION),
                    errorFieldClass: "errorField",
                    rules: ["digits_only,ID,Carnet es numerico!",
                        "required,NAME,Debe buscar un estudiante!",
                        "function,validaCursos"],
                    displayType : "display-html",
                    errorHTMLItemBullet : "&#8212; ",
                    onCompleteHandler:function(){
                        $('operation').value='prev';
                        waitMsg("Guardando...");
                        $(document.ASIGNACION).request({onComplete:function(r){
                                if(r.responseJSON.ST==1){
                                    $('operation').value='s';
                                    $(document.ASIGNACION).submit();
                                }else{
                                    alert(r.responseJSON.ERROR);
                                }
                                closeWait();
                            }});

                    }});
            }

            addOnLoad(config);

            addOnLoad(function(){
                var f=new Hash({tittle:'Listado de Alumnos',
                    url:'data.php?operation=stud',
                    callback:selStudent,width:500,height:500});
                setList($('buscar'),f.toObject());
            });
            
            validaCursos=function(){
                var error=new Array();
                if(cursos.size()==0){
                    error.push([$("PENSUM"), "No ha seleccionado cursos para la asignacion!"]);
                }
                if(error.size()>0){
                    return error.toArray();
                }
                return true;
            }
            selStudent=function(s){
                $("ID").value=s.CARNET;
                $("AIU_CODE").value=s.AIU_CODE;
                $("NAME").value=s.NOMBRE_A+' '+s.NOMBRE_B+' '+s.APELLIDOS;
            }


        </script>
    </head>

    <body>
        <div id="tabs">
            <ul class="nav nav-tabs ">
                <li class="active">
                    <a>ASIGNACION ESTUDIANTES</a>
                </li>
            </ul>
        </div>
        <div id="tab-cont">
            <div id="rsvErrors"></div>
            <form id="ASIGNACION" name="ASIGNACION" method="GET" action="data.php">
                <input type="hidden" name="cursos" id="cursos" />
                    <table class=""  width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <th width="50%">
                                &nbsp;
                            </th>
                            <th width="50%">
                                <input type="button" name="cmdOk" id="cmdOk" class="btn btn-info" onClick="rsv.validate();" value="Asignar"/><br/>
                            </th>
                        </tr>
                        <tr>
                            <td colspan="2"><br/><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <td width="20%"> NO. CARNET</td>
                                        <td width="80%">            <input name="ID" type="text" readonly="readonly" id="ID" />
                                            <input name="buscar" type="button" class="btn btn-small" id="buscar"  value="..." />            </td>
                                    </tr>
                                    <tr>
                                        <td>AIU CODIGO</td>
                                        <td>            <input name="AIU_CODE" readonly="readonly" type="text" id="AIU_CODE" /></td>
                                    </tr>

                                    <tr>
                                        <td>NOMBRE ESTUDIANTE</td>
                                        <td><input name="NAME" type="text" id="NAME" readonly="readonly" size="50" /></td>
                                    </tr>
                                    <td>CICLO</td>
                                    <td><select name="CYCLE" id="CYCLE">
                                            <option value="">Seleccione</option>
                                            <?php foreach ($listCycles as $it) { ?>
                                                <option value="<?php echo $it["CODE"]; ?>" <?= $user->cycle == $it["CODE"] ? 'selected="selected"' : '' ?>><?php echo $it["CODE"]; ?> </option>
                                            <?php } ?>
                                        </select></td>
                                    <tr>
                                        <td>CARRERA</td>
                                        <td><select name="CARRERA" id="CARRERA" onchange="datosCarrera(this);">
                                                <option value="">Seleccione</option>
                                                <?php foreach ($list as $it) { ?>
                                                    <option value="<?php echo $it["ID"]; ?>"><?php echo $it["NAME"]; ?> </option>
                                                <?php } ?>
                                            </select>          </td>
                                    </tr>
                                    <tr>
                                        <td>MES INICIO</td>
                                        <td>
                                            <select name="STARTMONTH" id="STARTMONTH">
                                                <option value="1" selected="selected">Enero</option>
                                                <option value="2">Febrero</option>
                                                <option value="3">Marzo</option>
                                                <option value="4">Abril</option>
                                                <option value="5">Mayo</option>
                                                <option value="6">Junio</option>
                                                <option value="7">Julio</option>
                                                <option value="8">Agosto</option>
                                                <option value="9">Septiembre</option>
                                                <option value="10">Octubre</option>
                                                <option value="11">Noviembre</option>
                                                <option value="12">Diciembre</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>PENSUM CARRERA</td>
                                        <td id="pensums"><select onchange="showCursos(this.value);" name="PENSUM" id="PENSUM">
                                            </select>          </td>
                                    </tr>
                                    <tr>

                                    </tr>
                                    <tr>
                                        <td id="cursosx" colspan="2" align="center">&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="center"><div id="cargos" style="width:700px;"></div>
                                            <script type="text/javascript">$('cargos').hide();</script>          </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">&nbsp;</td>
                                    </tr>


                                </table></td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <input type="hidden" name="operation" id="operation" /></td>
                        </tr>

                    </table>

                    <!-- cursos list-->
                    <div id="cursosList" style="width:60%; margin:0 auto; font-size: 1em;" ></div>

            </form>
        </div>
    </body>
</html>

<?php
if (isset($_GET['asigned'])) {
    echo '<script type="text/javascript"> new Ajax.Updater("cursosList","res.php",{parameters:"ASIGNED="+' . $_GET["asigned"] . '}); </script>';
    unset($_GET);
}
?>
